{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "ae2a25b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown\n",
    "from openai import OpenAI\n",
    "from pypdf import PdfReader\n",
    "import os\n",
    "import gradio as gr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "2eb947db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "df80c9c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Api key for openai is found and starts with: sk-proj-\n",
      "APi key for groqai is found and starts with: gsk_Vopn\n"
     ]
    }
   ],
   "source": [
    "openai = os.getenv(\"OPENAI_API_KEY\")\n",
    "groqai = os.getenv(\"groq_api_key\")\n",
    "\n",
    "if openai:\n",
    "    print(f\"Api key for openai is found and starts with: {openai[:8]}\")\n",
    "else:\n",
    "    print(\"key noy found.Check guide\")\n",
    "if groqai:\n",
    "    print(f\"APi key for groqai is found and starts with: {groqai[:8]}\")\n",
    "else:\n",
    "    print(\"groq api key not found\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "15823b9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    summary = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "cb071934",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = PdfReader(\"me/Profile.pdf\")\n",
    "\n",
    "linkedin= \"\"\n",
    "for page in reader.pages:\n",
    "    text = page.extract_text()\n",
    "    if text:\n",
    "        linkedin += text\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ec4be66",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Oluwatosin\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "77dbbe48",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = f\"You are asking question about {name} website,\\\n",
    "particularly questions related to {name} career , background, skills and experience.\\\n",
    "Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
    "You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
    "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
    "If you don't know the answer, say so.\"\n",
    "\n",
    "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
    "system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "0520c483",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "def chat(message, history):\n",
    "\n",
    "    message = [{\"role\":\"system\",\"content\":system_prompt}] + history + [{\"role\":\"user\",\"content\":message}]\n",
    "\n",
    "    response = openai.chat.completions.create(\n",
    "        model = \"gpt-4o-mini\",\n",
    "        messages = message\n",
    "    )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "f259aa57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7873\n",
      "* To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7873/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.ChatInterface(chat, type=\"messages\").launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1b9e902",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Time to evaluate the model - Aim is to build a Multi-LLM pipeline\n",
    "#We will use the groqapi to evaluate the openai model\n",
    "\n",
    "#First import a pydantc library and a basemodel class\n",
    "\n",
    "from pydantic import BaseModel\n",
    "\n",
    "class Evaluation(BaseModel):\n",
    "    is_acceptable: bool\n",
    "    feedback: str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "b58324ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "#create an evaluator variable\n",
    "\n",
    "evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
    "You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n",
    "The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
    "The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
    "The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
    "\n",
    "evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
    "evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "ae60c71f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluator_user_prompt(reply, message, history):\n",
    "    user_prompt = f\"Here's the conversation between the user and the agent:\\n\\n{history}\\n\\n\"\n",
    "    user_prompt += f\"Here's the latest message from the user:\\n\\n{message}\\n\\n\"\n",
    "    user_prompt +=f\"Here's the latest response from the agent:\\n\\n{reply}\\n\\n\"\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "5ce823c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#import and set enviroment for the groqai\n",
    "\n",
    "groqapi = OpenAI(api_key=groqai,\n",
    "                base_url=\"https://api.groq.com/openai/v1\"\n",
    "                )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d45762b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluate(reply, message, history) -> Evaluation:\n",
    "    messages = [{\"role\":\"system\",\"content\":evaluator_system_prompt}] + [{\"role\":\"user\",\"content\":evaluator_user_prompt(reply,message,history)}]\n",
    "    response = groqapi.chat.completions.create(\n",
    "        model=\"llama3-8b-8192\",\n",
    "        messages = messages,\n",
    "        #response_format=Evaluation\n",
    "    )\n",
    "\n",
    "    raw_content = response.choices[0].message.content\n",
    "\n",
    "    try:\n",
    "        # If response is a JSON string: {\"is_acceptable\": true, \"feedback\": \"...\"}\n",
    "        #using this deprectaed - evaluation = Evaluation.parse_raw(raw_content) - deprecated\n",
    "        evaluation = Evaluation.model_validate_json(raw_content)\n",
    "    except:\n",
    "        # Otherwise, fallback to plain text evaluation if it's not JSON\n",
    "        evaluation = Evaluation(\n",
    "            is_acceptable=\"acceptable\" in raw_content.lower(),\n",
    "            feedback=raw_content\n",
    "        )\n",
    "\n",
    "    return evaluation\n",
    "\n",
    "\n",
    "    #return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "1244b136",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages = [{\"role\":\"system\", \"content\":\"system_prompt\"}] + [{\"role\":\"user\", \"content\":\"do you hold a patent\"}]\n",
    "response = openai.chat.completions.create(\n",
    "    model = \"gpt-4o-mini\",\n",
    "    messages = messages\n",
    ")\n",
    "\n",
    "reply = response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "421c95ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Evaluation(is_acceptable=True, feedback='I evaluate the latest response from the agent as ACCEPTABLE.\\n\\nThe response is well-structured, concise, and directly addresses the user\\'s question. The agent acknowledges that they don\\'t hold a patent, which is an honest and clear answer. Additionally, the agent proactively offers to provide information on patents, application processes, or discuss patent law if the user has further questions, showing their willingness to engage and be helpful.\\n\\nFeedback:\\nThe response effectively addresses the user\\'s query, and the agent\\'s tone is professional and engaging. However, to further improve, the agent could consider adding a brief sentence or phrase to emphasize their expertise in the field of Data and AI, such as \"As a Data and AI Practitioner, I can provide insights on the patent process in my area of specialization.\" This would help to reinforce their credibility and expertise while maintaining the response\\'s overall length.')"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluate(reply,\"do you hold a patent?\",messages[:1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "2cf1d9c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def rerun(reply,message,history,feedback):\n",
    "\n",
    "    updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
    "    updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
    "    updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
    "    messages = [{\"role\":\"system\", \"content\":updated_system_prompt}] + history + [{\"role\":\"user\",\"content\":message}]\n",
    "    response = openai.chat.completions.create(\n",
    "        model = \"gpt-4o-mini\",\n",
    "        messages = messages\n",
    "    )\n",
    "    response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "0f714a8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def chat(message, history):\n",
    "    if \"patent\" not in message:\n",
    "        system = system_prompt + \"\\n\\n Everything in the reply needs to be in pig latin.It is mandatory that you respond and only entirely in pig latin\"\n",
    "    else:\n",
    "        system = system_prompt\n",
    "    messages = [{\"role\":\"system\",\"content\":system}]+ history + [{\"role\":\"user\", \"content\":message}]\n",
    "    response = openai.chat.completions.create(\n",
    "        model=\"gpt-4o-mini\",\n",
    "        messages = messages\n",
    "    )\n",
    "\n",
    "    reply = response.choices[0].message.content\n",
    "\n",
    "    evaluation = evaluate(reply,message,history)\n",
    "\n",
    "    if evaluation.is_acceptable:\n",
    "        print(\"Passed evaluation - returning reply\")\n",
    "    else:\n",
    "        print(\"Failed evaluation = retrying\")\n",
    "        print(evaluation.reply)\n",
    "        reply = rerun(reply, message, history, evaluation.feedback)\n",
    "    return reply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "3bcbca87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7877\n",
      "* To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7877/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Passed evaluation - returning reply\n"
     ]
    }
   ],
   "source": [
    "gr.ChatInterface(chat, type=\"messages\").launch()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
